package com.zeng.springcloud.provider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.bus.event.Destination;
import org.springframework.cloud.bus.event.PathDestinationFactory;
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author zyb
 * @date 2021-06-06 23:33
 */
@SpringBootApplication
@RemoteApplicationEventScan(basePackageClasses = MyEvent.class)
@EnableScheduling
public class ProviderService {
    @Autowired
    private ApplicationContext applicationContext;

    @Scheduled(fixedRate = 10000L)
    public void scheduled() {
        System.out.println("消息发送");
        String busId = applicationContext.getEnvironment().getProperty("spring.cloud.bus.id");
        MyEvent myEvent = new MyEvent(busId, "hello world!");
        applicationContext.publishEvent(myEvent);
        System.out.println("消息发送完成");
    }

    public static void main(String[] args) {
        SpringApplication.run(ProviderService.class, args);
    }
}
